var bmap = require('../../libs/bmap-wx.js');
var app = getApp();
var WxSearch = require('../../wxSearchView/wxSearchView.js');


Page({

  data: {

    navbar: ['今天', '其他'],
	menulist:['明天','后天'],
    currentTab1:0,
	currentTab2:-1,
    weatherData: '',
	maybe:false,
	weatherDatalist:['0','0','0'],
	nowdata:0


  },
  onLoad: function () {
	  var that = this;
    WxSearch.init(
      that,  // 本页面一个引用
      ['杭州', '嘉兴', "海宁", "桐乡", '宁波', '金华'], // 热点搜索推荐，[]表示不使用
      ['湖北', '湖南', '北京', "南京"],// 搜索匹配，[]表示不使用
      that.mySearchFunction, // 提供一个搜索回调函数
      that.myGobackFunction //提供一个返回回调函数
    );

    var that = this;
    // 新建百度地图对象 
    var BMap = new bmap.BMapWX({
      ak: 'EyFhMERn9r7Nadp5EBP56B34tv64KU6k'
    });
    var fail = function (data) {
      console.log(data)
    };
    var success = function (data) {
      var weatherData = data.currentWeather[0];
	  console.log(data.currentWeather);
	  console.log(data.originalData);
      weatherData = '城市：' + weatherData.currentCity + '\n' + 'PM2.5：' + weatherData.pm25 + '\n' + '日期：' + weatherData.date + '\n' + '温度：' + weatherData.temperature + '\n' + '天气：' + weatherData.weatherDesc + '\n' + '风力：' + weatherData.wind + '\n';
	  let tr0='weatherDatalist['+0+']';
	  let tr1='weatherDatalist['+1+']';
	  let tr2='weatherDatalist['+2+']';
	  that.setData({
		 [tr0]:data.currentWeather[0],
        [tr1]: data.originalData.results[0].weather_data[1],
		[tr2]:data.originalData.results[0].weather_data[2],
      });
	  that.setData({
		  nowdata:that.data.weatherDatalist[0]
	  });
    };
    // 发起weather请求 
    BMap.weather({
      fail: fail,
      success: success
    });
  },
  navbarTap: function (e) {
    var that=this;
    this.setData({

      currentTab1: e.currentTarget.dataset.idx,
	  maybe:(!that.data.maybe)

    });
	if(this.data.currentTab1==0)
		this.setData({
			nowdata:that.data.weatherDatalist[0]
		});

  },
  navbarsecond:function(e){
	  var that=this;
	  var i =e.currentTarget.dataset.id;
	  this.setData({

      currentTab2: e.currentTarget.dataset.id,
	  nowdata:that.data.weatherDatalist[i+1]
	  

    });
  },
  // 3 转发函数，固定部分，直接拷贝即可
  wxSearchInput: WxSearch.wxSearchInput,  // 输入变化时的操作
  wxSearchKeyTap: WxSearch.wxSearchKeyTap,  // 点击提示或者关键字、历史记录时的操作
  wxSearchDeleteAll: WxSearch.wxSearchDeleteAll, // 删除所有的历史记录
  wxSearchConfirm: WxSearch.wxSearchConfirm,  // 搜索函数
  wxSearchClear: WxSearch.wxSearchClear,  // 清空函数

  // 4 搜索回调函数  
  mySearchFunction: function (value) {
	  var that=this;
    // do your job here
    // 示例：跳转
		
	wx.request({
  url: 'https://api.map.baidu.com/geocoder/v2/?address=$'+value+'&output=json&ak=iIVqAxNKsDT0UauthpPk1TiA0RBt3aHp',
  data: {
    x: '',
    y: ''
  },
  header: {
    'content-type': 'application/json' // 默认值
  },
  success (res) {
    console.log(res.data);
    	that.setData({
			local:res.data.result.location.lng.toFixed(2)+','+res.data.result.location.lat.toFixed(2)
		});
  },
});
  var that = this;
    // 新建百度地图对象 
    var BMap = new bmap.BMapWX({
      ak: 'EyFhMERn9r7Nadp5EBP56B34tv64KU6k'
    });
	var location=that.data.local;
	console.log(location);
    var fail = function (data) {
      console.log(data)
    };
    var success = function (data) {
     console.log(data),
	 that.setData({
			searchresult:data.currentWeather[0]
		});
  
    };
    // 发起weather请求 
    BMap.weather({
      fail: fail,
      success: success,
	  location:location
    });
  },


  // 5 返回回调函数
  myGobackFunction: function () {
    // do your job here
    // 示例：返回
  var that=this;
    this.setData({

      currentTab1: 0,
maybe:(!that.data.maybe)

    });
	if(this.data.currentTab1==0)
		this.setData({
			nowdata:that.data.weatherDatalist[0]
		});
  }

})
